﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using System.Linq;

namespace QuanLyKTX
{
    using DAL;
    using Utilities;
    using Telerik.WinControls.UI.Localization;
    using Telerik.WinControls.UI;
    public partial class frm_RoomBill : Telerik.WinControls.UI.RadForm
    {

        #region Private Methods

        //private RadContextMenu contextMenu;

        private void BindGridView(byte kyThang, string nam)
        {
            using (var db = new QuanLyKTXDataContext())
            {

                var results = from p in db.tbl_HoaDonPhongs
                              join q in db.tbl_HopDongs on p.MaHopDong equals q.MaHopDong
                              join r in db.tbl_SinhViens on q.ID equals r.ID
                              join s in db.tbl_XepPhongs on r.ID equals s.ID
                              orderby p.NgayThanhToan ascending
                              where p.KyThang <= kyThang && p.Nam == nam && p.DaThanhToan == false
                              select new
                              {
                                  p.MaHoaDon,
                                  p.MaHopDong,
                                  p.KyThang,
                                  p.Nam,
                                  p.NgayThanhToan,
                                  q.NgayBatDauHD,
                                  q.NgayKetThucHD,
                                  r.MaSinhVien,
                                  HoTen = r.Ho + " " + r.Ten,
                                  s.SoPhong
                              };                
                radGridView_ChuaThu.DataSource = results;
                radGridView_ChuaThu.AutoGenerateColumns = false;
            }
        }

        private void ResetForm()
        {
            //radTextBox_MaSV.Clear();
            //radTextBox_HoTen.Clear();
            //radTextBox_Ten.Clear();
            //radTextBox_GhiChu.Clear();
            //radTextBox_MaSV.Focus();
        }

        private void InsHoaDonPhong(Guid maHopDong, decimal tienPhong)
        {
            //using (var db = new QuanLyKTXDataContext())
            //{
            //    tbl_HoaDonPhong hoaDon = new tbl_HoaDonPhong();
            //    hoaDon.MaHoaDon = Guid.NewGuid();
            //    hoaDon.MaHopDong = maHopDong;
            //    hoaDon.NgayThanhToan = DateTime.Now;
            //    hoaDon.SoTien = tienPhong;
            //    //hoaDon.GhiChu = radTextBox_GhiChu.Text;

            //    db.tbl_HoaDonPhongs.InsertOnSubmit(hoaDon);
            //    db.SubmitChanges();
            //}
        }

        //private void UpdHoaDonPhong(Guid maHoaDon, bool daThanhToan)
        //{
        //    using (var db = new QuanLyKTXDataContext())
        //    {
        //        var result = (from p in db.tbl_HoaDonPhongs
        //                      where p.MaHoaDon == maHoaDon
        //                      select p).FirstOrDefault();

        //        result.DaThanhToan = daThanhToan;
        //        db.SubmitChanges();

        //        BindGridView();

        //    }
        //}

        //private void InitContextMenu()
        //{
        //    contextMenu = new RadContextMenu();
        //    RadMenuItem menuItemIn = new RadMenuItem("Xem trước và in");
        //    menuItemIn.Click += new EventHandler(menuItemIn_Click);

        //    contextMenu.Items.Add(menuItemIn);
        //}

        private void DefaultLoadForm()
        {
            // Khởi tạo Kỳ tháng
            for (int i=1; i <= 12; i++)
            {
                RadListDataItem dataItem = new RadListDataItem();
                dataItem.Value = i;
                dataItem.Text = i.ToString();

                radDropDownList_KyThang.Items.Add(dataItem);
            }
            radDropDownList_KyThang.Items[DateTime.Now.Month-1].Selected=true;

            // Khởi tạo Năm
            radMaskedEditBox_Nam.Value = DateTime.Now.Year;           

            radDropDownList_KyThang.Focus();
        }

        #endregion

        void menuItemIn_Click(object sender, EventArgs e)
        {
            frm_ReportRoomBill rRoomBill = new frm_ReportRoomBill(radGridView_ChuaThu.CurrentRow.Cells[0].Value.ToString());
            rRoomBill.ShowDialog();
        }

        public frm_RoomBill()
        {
            InitializeComponent();
            RadGridLocalizationProvider.CurrentProvider = new MyCustomRadGridLocalizationProvider();
        }

        private void frm_RoomBill_Load(object sender, EventArgs e)
        {
            DefaultLoadForm();
            BindGridView(Convert.ToByte(radDropDownList_KyThang.SelectedValue), radMaskedEditBox_Nam.Value.ToString());
            //InitContextMenu();
        }

        private void radButton_Thoat_Click(object sender, EventArgs e)
        {
            Close();
        }        

        //private void radGridView_ChuaThu_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        //{
        //    if (!radGridView_ChuaThu.CurrentRow.IsSystem)
        //    {
        //        e.ContextMenu = contextMenu.DropDown;
        //    }
        //}

        //private void radGridView_ChuaThu_ValueChanged(object sender, EventArgs e)
        //{
        //    if (radGridView_ChuaThu.ActiveEditor is RadCheckBoxEditor)
        //    {
        //        Guid maHoaDon = new Guid(radGridView_ChuaThu.CurrentRow.Cells[0].Value.ToString());
        //        //UpdHoaDonPhong(maHoaDon, Convert.ToBoolean(radGridView_ChuaThu.ActiveEditor.Value));
        //        //BindGridView();
        //    }
        //}

        private void radDropDownList_KyThang_Enter(object sender, EventArgs e)
        {
            ((Telerik.WinControls.UI.RadDropDownListEditableAreaElement)(this.radDropDownList_KyThang.GetChildAt(0).GetChildAt(0))).BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(253)))), ((int)(((byte)(215)))));
        }

        private void radDropDownList_KyThang_Leave(object sender, EventArgs e)
        {
            ((Telerik.WinControls.UI.RadDropDownListEditableAreaElement)(this.radDropDownList_KyThang.GetChildAt(0).GetChildAt(0))).BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
        }

        private void radDropDownList_KyThang_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            if (radMaskedEditBox_Nam.Value.ToString() != string.Empty)
            {
                BindGridView(Convert.ToByte(radDropDownList_KyThang.SelectedValue.ToString()), radMaskedEditBox_Nam.Value.ToString());
            }
            else
            {
                radMaskedEditBox_Nam.Focus();
            }
        }

        private void radMaskedEditBox_Nam_ValueChanged(object sender, EventArgs e)
        {
            if (radMaskedEditBox_Nam.Value.ToString() != string.Empty)
            {
                BindGridView(Convert.ToByte(radDropDownList_KyThang.SelectedValue.ToString()), radMaskedEditBox_Nam.Value.ToString());
            }
            else
            {
                radMaskedEditBox_Nam.Focus();
            }
        }
    }
}
